package com.smec.remes.module.scan;

import android.content.Context;
import android.content.SharedPreferences;

import com.smec.moblie.SMECApplication;
import com.smec.moblie.activity.LoginActivity;
import com.smec.remes.base.SmecBasePresenter;
import com.smec.remes.module.scan.beans.SmecAllocOrder;
import com.smec.remes.module.scan.beans.SmecOutboundOrder;
import com.smec.remes.smecManager.SmecRetrofit.BaseSubscriber;
import com.smec.remes.smecManager.SmecRetrofit.HttpResult;

import java.util.WeakHashMap;

import rx.Observable;

/**
 * Created by xupeizuo on 2018/3/28.
 */

public class SmecScanPresenter extends SmecBasePresenter{

    public static final String GET_OUT_SUCCESS="GET_OUT_SUCCESS";
    public static final String GET_OUT_FAILURE="GET_OUT_FAILURE";

    public static final String GET_ALLOC_SUCCESS="GET_ALLOC_SUCCESS";
    public static final String GET_ALLOC_FAILURE="GET_ALLOC_FAILURE";

    public static final String MAKE_SURE_SUCCESS="MAKE_SURE_SUCCESS";
    public static final String MAKE_SURE_FAILURE="MAKE_SURE_FAILURE";

    public SmecScanPresenter(Context mContext) {
        super(mContext);
    }

    /**
     * 获取出库单信息
     * @param orderNo
     */
    public void getOutOrderMsg(String orderNo){
        WeakHashMap<String,Object> map=new WeakHashMap<>();
        map.put("outboundOrderNo",orderNo);
        Observable<HttpResult<SmecOutboundOrder>> observable=smecPackingService.getOutOrderMsg(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<SmecOutboundOrder>>() {

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRxbus.post(GET_OUT_FAILURE,new Object());
            }

            @Override
            public void onNext(HttpResult<SmecOutboundOrder> smecOutboundOrderHttpResult) {
                mRxbus.post(GET_OUT_SUCCESS,smecOutboundOrderHttpResult.getData());
            }
        });
    }

    /**
     * 获取调拨单信息
     * @param orderNo
     */
    public void getAllocMsg(String orderNo){
        WeakHashMap<String,Object> map=new WeakHashMap<>();
        map.put("allocOrderNo",orderNo);
        Observable<HttpResult<SmecAllocOrder>> observable=smecPackingService.getAllocOrderMsg(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<SmecAllocOrder>>() {

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRxbus.post(GET_ALLOC_FAILURE,new Object());
            }

            @Override
            public void onNext(HttpResult<SmecAllocOrder> smecAllocOrderHttpResult) {
                mRxbus.post(GET_ALLOC_SUCCESS,smecAllocOrderHttpResult.getData());
            }
        });
    }

    /**
     *  确认收货
     * @param smecOutboundOrder
     */
    public void makeSure(SmecOutboundOrder smecOutboundOrder){
        SharedPreferences mySharedPreferences=
                SMECApplication.getContext().getSharedPreferences("ElevatorSharedPreferences", LoginActivity.MODE_PRIVATE);
        smecOutboundOrder.setToken(mySharedPreferences!=null ? mySharedPreferences.getString("TOKEN",""):"");
        Observable<HttpResult> observable=smecPackingService.makeSureOrder(smecOutboundOrder);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult>() {

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRxbus.post(MAKE_SURE_FAILURE,new Object());
            }

            @Override
            public void onNext(HttpResult httpResult) {
                mRxbus.post(MAKE_SURE_SUCCESS,new Object());
            }
        });
    }
}
